﻿using Md.Api.DDD;
using Md.Api.Model.Manage.SysUser.Request;
using Md.Dapper.Repositories.Md;

namespace Md.Api.Service.Manage.SysUser;
/// <summary>
/// 修改系统用户密码服务
/// </summary>
public class ForgetSysUserPwdService : ApiBase<RequestForgetSysUserPwdModel>
{
    /// <summary>
    /// 系统用户仓储
    /// </summary>
    public SysUserRep sysUserRep { get; set; }
    /// <summary>
    /// 执行
    /// </summary>
    protected override void ExecuteMethod()
    {
        var user = sysUserRep.GetSysUserPwdByUserId(Convert.ToInt64(userKey));

        if (user == null)
        {
            throw new AggregateException("此用户不存在");
        }

        if (user.DelFlag == "2")
        {
            throw new AggregateException("此用户不存在");
        }

        if (user.Status == "1")
        {
            throw new AggregateException("此用户已停用");
        }

        var oldPwd = EncrptyHelp.MD5Pwd(Parameter.OldPassword);

        if (user.Password != oldPwd)
        {
            throw new AggregateException("旧密码不正确");
        }

        var newPwd = EncrptyHelp.MD5Pwd(Parameter.NewPassword);

        var res = sysUserRep.UpdateSysUserPwdByUserId(Convert.ToInt64(userKey), newPwd);

        if (res > 0)
        {
            Result.Data = sysUserRep.GetSysUserByUserId(Convert.ToInt64(userKey));
        }
    }
}

